package com.kgc.crowd.distributioncrowddatabaseprovider.controller;

import com.kgc.crow.distributioncrowdcommon.pojo.MemberLaunchInfoPO;
import com.kgc.crow.distributioncrowdcommon.pojo.MemberPo;
import com.kgc.crow.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crow.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crow.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/database/provider")
public class MemberController {

    @Resource
    MemberService memberService;

    //根据账号查询该账号是否存在
    @RequestMapping("/retrieve/loign/acct/count")
    public ResultEntity<Integer> retrieveLoignAcctCount(
            @RequestParam("loginAcct") String loginAcct) {
        // 判断账号是否存在
        if (!CrowdUtils.strEffectiveCheck(loginAcct)) {
            return ResultEntity.failed(CrowdConstant.MESSAGE_LOGINACCT_INVALID);
        }
        try {
            int count = memberService.getLoignAcctCount(loginAcct);

            return ResultEntity.successWithData(count);

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }

    //添加
    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMemberRemote(@RequestBody MemberPo memberPO) {
        try {
            // 执行保存
            memberService.saveMemberPO(memberPO);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

        return ResultEntity.successNoData();
    }

    //根据账号查Member对象
    @RequestMapping("/retrieve/member/by/login/acct")
    public ResultEntity<MemberPo> retrieveMemberByLoginAcct(@RequestParam("loginAcct") String loginAcct) {

        MemberPo memberPO = null;

        try {
            memberPO = memberService.getMemberByLoginAcct(loginAcct);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

        return ResultEntity.successWithData(memberPO);
    }

    @RequestMapping("/retrieve/member/launch/info/po")
    public ResultEntity<MemberLaunchInfoPO> resultEntity(@RequestParam("memberid") Integer memberid) {
        try {
            MemberLaunchInfoPO memberLaunchInfoPOByMember = memberService.getMemberLaunchInfoPOByMember(memberid);
            return ResultEntity.successWithData(memberLaunchInfoPOByMember);
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }
}
